package com.bluemson.filter;

import com.bluemson.entity.User;
import com.bluemson.service.CheckService;
import com.bluemson.service.Impl.CheckServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//@WebFilter(filterName = "PermitFilter")
public class PermitFilter implements Filter {
    @Override
    public void init(FilterConfig config) throws ServletException {
    }

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;
        Map map = new HashMap();
        String uri = req.getRequestURI();
        HttpSession session = req.getSession();
        User currentUser = (User) session.getAttribute("currentUser");
        CheckService checkService = new CheckServiceImpl();
        if(currentUser.getIdentity()==0&&checkService.checkHost(currentUser.getUsername())){
            if(uri.contains("manage")||uri.contains("allUser")){
                resp.setStatus(302);
                return;
            }
        }
        chain.doFilter(request, response);
    }
}
